//home.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({
  data: {
    navTab: ['发现', '专题', '大赛'],
    currentNavtab:'0',
    imgUrls: [],
    indicatorDots: true,//是否显示面板指示点
    autoplay: true,//是否自动切换
    interval: 5000,//自动切换时间间隔
    duration: 1000, //滑动动画时长
    details: [],
    page: 1,
    canUplod: true,//是否可以上拉加载
    url:null,
    data:null,
    tabbars:[
      {title: '首页', icon: '/image/tab/squareSelect.png', color:'#0084ff'},
      { title: '创作', icon: '/image/tab/createImage.png', color: '#000000'},
      { title: '我的', icon: '/image/tab/mineNoSelect.png', color: '#000000' }]
  },
  onLoad: function () {
    console.log('onLoad')
    //隐藏tabbar
    wx.hideTabBar({
      animation:false
    }),
    //网络请求获取数据
    this.getGuideImage();//发现 轮播图

    //默认url 和data
    this.setData({
      url: '/api/v1/knowledge/webview/fine/',
      data: {
        page_size_count: 5,
        page: this.data.page
      },
    });
    this.getDetailLists();//网络请求 默认发现
  },

  //tabbar的点击事件
  tabbarClick:function(event){
    console.log('切换tab' + event.currentTarget.dataset.idx)
    if (event.currentTarget.dataset.idx == 0){
    } else if (event.currentTarget.dataset.idx == 1){
      //创作
      wx.navigateTo({
        url: '../production/production?receiveModel=' + '', 
      })
    }else{
      //我的
      wx.switchTab({
        url: '../me/me'
      })
    }
  },

  //获取发现，大赛 专题 列表
  getDetailLists:function () {
    wx.showNavigationBarLoading();
    var that = this;
    wx.request({
      url: util.baseurl + that.data.url,
      data: that.data.data,
      method: 'GET',
      success: function (res) {
        //赋值
        if (res.data.code === 0) {
          if (that.data.page === 1) {
            //清空数据
            that.setData({
              details: []
            })
          }
          var newArr = that.data.details.concat(res.data.detail.results)
          if (res.data.detail.next) {
            that.setData({
              canUplod: true,
              page: that.data.page + 1,
              details: newArr
            })
          } else {
            that.setData({
              canUplod: false,
              page: that.data.page,
              details: newArr
            })
          }
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络开小差了',
          icon: 'none'
        })
      },
      complete: function () {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  //获取轮播图数据
  getGuideImage:function(){
    var that = this;
    wx.request({
      url: util.baseurl + '/api/v1/knowledge/webview/uptop-topic/?query_type=1',
      method: 'GET',
      success: function (res) {
        //赋值
        if (res.data.code === 0) {
            that.setData({
              imgUrls: res.data.detail.uptop_works
            })
        }
      }
    })
  },

  scrolltolower:function(){
    console.log('上拉加载')
    if (this.data.canUplod) {
      this.setData({
        url: this.data.url,
        data: {
          page_size_count: 5,
          page: this.data.page
        },
      });
      this.getDetailLists();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },

  upper: function () {
  },

//下拉刷新
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      url: this.data.url,
      page: 1,
      data: {
        page_size_count: 5,
        page:1
      },
    });
    this.getDetailLists();
  },

  switchTab:function(e){
    console.log('切换tab')
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    if (e.currentTarget.dataset.idx === 0){
      //发现
      this.setData({
        url: '/api/v1/knowledge/webview/fine/',
        page:1,
        data: {
          page_size_count: 5,
          page: 1
        },
      });
      this.getDetailLists();
    } else if (e.currentTarget.dataset.idx === 1) {
      //专题
      this.setData({
        url: '/api/v1/knowledge/webview/subject/',
        page: 1,
        data: {
          page: 1
        },
      });
      this.getDetailLists();
    }else{
      //大赛
      this.setData({
        url: '/api/v1/knowledge/wk/competition/',
        page: 1,
        data: {
          page: 1
        },
      });
      this.getDetailLists();//网络请求 默认发现
    }

  },
  //前往搜索
  toSearch: function (event){
    wx.navigateTo({
      url: '../search/search'
    })
  },

  //轮播图的点击事件
  guideItemClick: function (e) {
    //要根据type来
    var idStr = String(e.target.dataset.postid);
    var titleStr = String(e.target.dataset.title);
    wx.navigateTo({
      url: `../projectList/projectList?kgeId=${idStr}&title=${titleStr}&type=0`
    })
  },
   //专题的点击事件 跳到专题列表
  chooseSkin: function(e) {
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.title);
    console.log('查看作品' + idStr + titleStr);
    wx.navigateTo({
      url: `../projectList/projectList?kgeId=${idStr}&title=${titleStr}&type=0`
    })
  },
  //大赛
  chooseCompetition:function(e){
    //强转成字符串
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.name);
    console.log('微课大赛' + idStr + titleStr);
    wx.navigateTo({
      url: `../megagame/megagame?kgeId=${idStr}&title=${titleStr}`
    })
  },
  //点击查看详情
  viewFilmDetail: function (e) {
    //强转成字符串
    var idStr = String(e.currentTarget.dataset.id);
    var titleStr = String(e.currentTarget.dataset.title);
    console.log('查看作品' + idStr);
    wx.navigateTo({
      url: `../knowledge/knowledge?kgeId=${idStr}&title=${titleStr}`
      // url: '../knowledge/knowledge?kgeId=' + e.target.dataset.idx + '&title=' + e.target.dataset.title
    })
  },
})
